// import { login, logout, getInfo } from '@/api/login'
// import { removeToken, removeUser, removeRemeber, setUser, setRemeber } from '@/utils/auth'
// import {setToken} from "../../utils/auth";

// const user = {
//   state: {
//     token: '',
//     data: null
//   },

//   mutations: {
//     SET_TOKEN: (state, token) => {
//       state.token = token
//     },
//     SET_DATA: (state, data) => {
//       state.data = data
//     }
//   },

//   actions: {
//     // 登录
//     Login({ commit }, { username, password, remeberMe }) {
//       return new Promise((resolve, reject) => {
//         login(username, password).then(response => {
//           const data = response.data
//           commit('SET_DATA', data)
//           setUser(JSON.stringify(data))

//           const token = data.authentication
//           commit('SET_TOKEN', token)
//           setToken(token)

//           if (remeberMe)
//             setRemeber(btoa(JSON.stringify({username, password})))

//           resolve(data)
//         }).catch(error => {
//           reject(error)
//         })
//       })
//     },

//     // 获取用户信息
//     async GetInfo({ commit, state }) {
//       const data = (await getInfo()).data
//       commit('SET_DATA', data)
//       setUser(JSON.stringify(data))
//       return data
//     },

//     // 登出
//     LogOut({ commit, state }) {
//       return new Promise((resolve, reject) => {
//         logout(state.token).then(() => {
//           commit('SET_TOKEN', '')
//           commit('SET_DATA', null)
//           removeToken()
//           removeUser()
//           removeRemeber()
//           resolve()
//         }).catch(error => {
//           reject(error)
//         })
//       })
//     },

//     // 前端 登出
//     FedLogOut({ commit }) {
//       return new Promise(resolve => {
//         commit('SET_TOKEN', '')
//         removeToken()
//         resolve()
//       })
//     }
//   }
// }

// export default user


import { login, logout, getInfo } from '@/api/login'
import { removeToken, removeUser, removeRemeber, getUser,setUser, setRemeber } from '@/utils/auth'
import {setToken} from "../../utils/auth";
import { defineStore } from 'pinia'

const useUserStore = defineStore(
 'user',{
    state: ()=> ({
      id: '',
      token: '',
      username:'',
      roleName: null,
      data: null, 
      permissions: [],
      //roles: []
    }),

    // mutations: {
    //   SET_TOKEN: (state, token) => {
    //     state.token = token
    //   },
    //   SET_DATA: (state, data) => {
    //     state.data = data
    //   }
    // },

    actions: {
      // 登录
      Login({ username, password, remeberMe }) {
        return new Promise((resolve, reject) => {
          login(username, password).then(response => {
            const data = response.data  
            setUser(JSON.stringify(data)) 

            const token = data.authentication
            this.token = token
            this.id = data.id
            this.username = data.username
            this.roleName = data.roleName
            this.permissions = data.permissions 
            this.roles = data.roles
            setToken(token) 
            if (remeberMe){
              setRemeber(btoa(JSON.stringify({username, password, remeberMe}))) 
            }
             
            resolve(data)
          }).catch(error => {
            reject(error)
          })
        })
      },
 
      // 获取用户信息
        GetInfo() {
        // const data = (await getInfo()).data
        // console.log(data) 
        // this.id = data.id
        // this.username = data.username
        // this.roleName = data.roleName
         return getUser()
      },

      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          // logout(this.token).then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            removeToken()
            removeUser()
            removeRemeber()
            resolve()
          // }).catch(error => {
          //   reject(error)
          // })
        })
      },

      // 前端 登出
      fedLogOut() {
        return new Promise(resolve => {
          this.token = ''
          this.roles = []
          this.permissions = []
          removeToken()
          removeUser()
          removeRemeber()
          resolve()
        })
      }
    }
  }
)
  
export default useUserStore